const request=require("../../utils/request")
Page({
  data: {
    banner:[],
    posts:{},
    lastID:0
  },
  onLoad(){
    wx.showLoading({
      title: '正在加载',
    });
    request.get("/index/index").then(result=>{
      wx.hideLoading();
      this.setData({
        banner:result.banner,
        posts:result.posts,
        //一开始进入首页获取的数据post里面有id
        lastID:result.posts.lastid
      })
    })
  },
  onPullDownRefresh(){
    console.log("donw.............")
    // wx.startPullDownRefresh();//开启下拉刷新动画 如果在onPullDownRefresh函数里面调这个函数，会死循环
    wx.showLoading({
      title: '正在加载...',
    });
    let oldPosts=this.data.posts;
    request.get(`/index/getIndexPosts/lastid/${this.data.lastID}`).then(result=>{
      wx.stopPullDownRefresh();//停止下拉动画
      //合并数组
      let p=result.list.concat(oldPosts.list);
          result.list=p;
      this.setData({
        posts:result,
        lastID:result.lastid
      });
      wx.hideLoading()
    });
  },
  onReachBottom(){
    wx.showLoading({
      title: 'Loading....',
    })
    let posts=this.data.posts;
    request.get(`/index/getIndexPosts/lastid/${this.data.lastID}`).then(result=>{
        //把当前获取最新数据和原来的数组合并 合并之后要重新赋值.
        posts.list=posts.list.concat(result.list);
        this.setData({
          posts:posts,
          lastID:result.lastid //重新赋值lastID
        });
        wx.hideLoading()
    })
  }
})